import Cookie from 'js-cookie'
export default {
  namespaced: true,
  state: {
    isCollapse: false,
    tabList: [
      {
        path: "/",
        name: "home",
        label: "首页",
        icon: "s-home",
        url: "Home/Home",
      }
    ],
    menu: []
  },
  mutations: {
    collapseMenu(state) {
      state.isCollapse = !state.isCollapse
    },
    selectMenu(state, val) {
      if (val.name !== 'home') {
        const index = state.tabList.findIndex(item => item.name === val.name)
        if (index === -1) {
          state.tabList.push(val)
        }
      }
    },
    closeTag(state, id) {
      state.tabList.splice(id, 1)
    },
    setMenu(state, val) {
      console.log(val, 'ssss');
      state.menu = val
      Cookie.set('menu', JSON.stringify(val))
    },
    addMenu(state, router) {
      if (!Cookie.get('menu')) return
      const menu = JSON.parse(Cookie.get('menu'))
      state.menu = menu
      const menuArray = []
      menu.forEach(item => {
        if (item.children) {
          item.children = item.children.map(it => {
            it.component = () => import(`../views/${it.url}`)
            return it
          })
          menuArray.push(...item.children)
        } else {
          item.component = () => import(`../views/${item.url}`)
          menuArray.push(item)
        }
      });
      console.log(menuArray, 'MA');
      menuArray.forEach(item => {
        console.log(item);
        router.addRoute('Main', item)
      })
    }
  }
}